<?php
/**
 * 通知
 * 
 * @package ews
 * @ews Enterprise WorkShop
 * @version 1.0
 * @since 2011-06-07
 * @author monkee
 */
class noticeModel extends model
{
	/**
	 * 添加新通知
	 * 
	 * @param int $user_id
	 * @param string $message
	 * @return int 通知ID
	 */
	public function create($user_id, $message){
		$id = DB::insert('notice', array(
			'user_id' => $user_id,
			'message' => $message,
			'mktime' => TIMESTAMP
		));
		//if($id > 0){
			DB::columnAdd('user', 'unread_notice', "user_id = {$user_id}");
		//}
		return $id;
	}
	/**
	 * 获取通知列表
	 * 
	 * @param int $user_id
	 * @return array 列表信息
	 */
	public function get_unread($user_id){
		$row = DB::fetchArray('SELECT * FROM '.DB::table('notice')." WHERE user_id = {$user_id} LIMIT 15");
		return $row;
	}
	
	/**
	 * 删除通知
	 * 
	 * @param int $notice_id
	 * @return void
	 */
	public function delete($notice_id){
		$row = DB::fetchOne('SELECT * FROM '.DB::table('notice')." WHERE notice_id = {$notice_id} LIMIT 1");
		if($user_id > 0 && $row['user_id'] != $user_id){
			return array();
		}
		if(count($row) > 0){
			DB::columnAdd('user', 'unread_notice', "user_id = {$user_id}", -1);
		}
		DB::delete('notice', 'notice_id = '.$notice_id);
		return ;
	}
	
	/**
	 * 删除全部通知
	 * 
	 * @param int $user_id
	 * @return void
	 */
	public function delete_all($user_id){
		DB::delete('notice', 'user_id = '.$user_id);
		DB::update('user', array('unread_notice' => 0), 'user_id = '.$user_id);
	}
}
?>